package com.example.accessibilityhelper.producer

import com.example.accessibilityhelper.arch.TaskObserver

class TaskRecorder {
    val wechatIds = arrayListOf<String>()
    val completedIds = arrayListOf<String>()
    val observers = arrayListOf<TaskObserver>()

    init {
        wechatIds.addAll(listOf("原磨豆浆", "双儿姑娘", "砖业挨踢", "哈密瓜", "闹闹", "邓勇先"))
    }

    fun register(observer: TaskObserver) {
        observers.add(observer)
    }

    fun unregister(observer: TaskObserver) {
        observers.remove(observer)
    }

    fun complete(vararg ids: String) {
        wechatIds.removeAll(ids)
        completedIds.addAll(ids)
        val completed = wechatIds.isEmpty()
        val progress = completedIds.size
        if (completed) {
            notifyComplete()
        } else {
            notifyProgressChanged(wechatIds.size, progress)
        }
    }

    private fun notifyComplete() {
        for (observer in observers) {
            observer.onComplete()
        }
    }

    fun notifyProgressChanged(size: Int, progress: Int) {
        for (observer in observers) {
            observer.onProgressChanged(size, progress)
        }
    }
}